package com.ljf.daily.consumptiondetail.domain.validator;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljf.common.domain.validator.BasisValidator;
import com.ljf.common.exception.ServiceException;
import com.ljf.common.utils.CollectionUtil;
import com.ljf.common.utils.DateUtil;
import com.ljf.common.utils.StringUtil;
import com.ljf.daily.consumptiondetail.domain.repository.DailyDatConsumptionDetailRepository;
import com.ljf.daily.consumptiondetail.dto.command.DailyDatConsumptionDetailCommand;
import com.ljf.daily.consumptiondetail.dto.command.DailyDatConsumptionDetailGenerateDataCommand;
import com.ljf.daily.consumptiondetail.dto.query.DailyDatConsumptionDetailQuery;
import com.ljf.daily.consumptiondetail.model.DailyDatConsumptionDetail;
import com.ljf.daily.consumptiondetail.vo.DailyDatConsumptionDetailVO;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.stream.Collectors;

/**
 * 日常消费明细  业务校验
 *
 * @author LJF
 * @since 2024-05-15
 */
@Component
public class DailyDatConsumptionDetailValidator extends BasisValidator<DailyDatConsumptionDetailQuery, DailyDatConsumptionDetailCommand, DailyDatConsumptionDetailVO, DailyDatConsumptionDetail, DailyDatConsumptionDetailRepository> {

    /**
     * 生成数据接口 业务校验
     *
     * @param command
     * @throws ServiceException
     */
    public void checkGenerateData(DailyDatConsumptionDetailGenerateDataCommand command) throws ServiceException {
        if (command.getStartDate() == null) {
            throw new ServiceException("生成数据异常:开始生成日期不能为空");
        }
        if (command.getEndDate() == null) {
            throw new ServiceException("生成数据异常:结束生成日期不能为空");
        }
        if (CollectionUtil.isNotEmpty(command.getSpecialDate())) {
            for (Date date : command.getSpecialDate()) {
                date = DateUtil.parse(DateUtil.formatDate(date) + " 00:00:00");
                if (date.getTime() < command.getStartDate().getTime()) {
                    throw new ServiceException(String.format("生成数据异常:特殊日期[%s]不能早于开始日期[%s]", DateUtil.formatDate(date), DateUtil.formatDate(command.getStartDate())));
                }
                if (date.getTime() > command.getEndDate().getTime()) {
                    throw new ServiceException(String.format("生成数据异常:特殊日期[%s]不能晚于结束日期[%s]", DateUtil.formatDate(date), DateUtil.formatDate(command.getEndDate())));
                }
            }
        }
        DailyDatConsumptionDetailQuery sameQuery = DailyDatConsumptionDetailQuery.builder()
                .belongDateStart(command.getStartDate())
                .belongDateEnd(command.getEndDate())
                .build();
        Integer sameCount = repository.count(sameQuery);
        if (sameCount > 0) {
            sameQuery.setCurrent(1);
            sameQuery.setSize(TIP_MAX_SIZE);
            Page<DailyDatConsumptionDetailVO> samePage = repository.selectPageList(sameQuery);
            String sameDate = samePage.getRecords().stream().map(one -> DateUtil.formatDate(one.getBelongDate())).collect(Collectors.joining(","));
            if (sameCount <= TIP_MAX_SIZE) {
                throw new ServiceException(String.format("生成数据异常:时间为[%s]有[%s]条记录已生成, 不可重复生成", sameDate, sameCount));
            }
            else {
                throw new ServiceException(String.format("生成数据异常:时间为[%s]等等有[%s]条记录已生成, 不可重复生成", sameDate, sameCount));
            }
        }
    }

    /**
     * 获取更新数据 业务校验
     *
     * @param date
     * @param dayConsumption
     * @throws ServiceException
     */
    public void checkEditData(Date date, String dayConsumption) throws ServiceException {
        if (date == null) {
            throw new ServiceException("获取更新数据校验异常: 参数缺失");
        }
        if (StringUtil.isNotEmpty(dayConsumption) && (!StringUtil.matches(dayConsumption, "^[\\d|\\s|.|-]*$"))) {
            throw new ServiceException("获取更新数据校验异常: 消费金额异常");
        }
    }

}
